import { 
  initializeZRender, 
  initializeTasks, 
  initializeMilestones, 
  initializeScrollPosition, 
  applyFilter,
  ContextMenu,
  $lastScrollXSpan
} from './modules/initialization.js';

import { 
  setupMouseWheelHandler, 
  setupResizeHandler, 
  setupClickHandler, 
  setupControlsHandler 
} from './modules/eventHandlers.js';

import { ChartRenderer } from './modules/chartRenderer.js';

/**
 * 甘特图应用主类
 */
class GanttApp {
  constructor() {
    this.zr = null;
    this.tasks = null;
    this.mileStones = null;
    this.scrollState = null;
    this.chartRenderer = null;
  }

  /**
   * 初始化应用
   */
  init() {
    // 初始化ZRender
    this.zr = initializeZRender();
    
    // 初始化数据
    this.tasks = initializeTasks();
    this.mileStones = initializeMilestones();
    this.scrollState = initializeScrollPosition();
    
    // 设置全局变量（保持向后兼容）
    window.tasks = this.tasks;
    window.mileStones = this.mileStones;
    
    // 创建图表渲染器
    this.chartRenderer = new ChartRenderer(this.zr, this.tasks, this.mileStones, this.scrollState);
    
    // 设置事件处理
    this.setupEventHandlers();
    
    // 应用过滤器
    applyFilter(this.tasks);
    
    // 初始绘制
    this.chartRenderer.redrawChart();
    
    // 设置全局函数（保持向后兼容）
    window.redrawChart = this.chartRenderer.redrawChart.bind(this.chartRenderer);
  }

  /**
   * 设置事件处理
   */
  setupEventHandlers() {
    // 设置鼠标滚轮事件
    setupMouseWheelHandler(this.zr, this.chartRenderer.redrawChart.bind(this.chartRenderer), this.scrollState);
    
    // 设置窗口大小变化事件
    setupResizeHandler(this.zr, this.chartRenderer.redrawChart.bind(this.chartRenderer));
    
    // 设置点击事件（隐藏ContextMenu）
    setupClickHandler(this.zr, ContextMenu);
    
    // 设置控制面板事件
    setupControlsHandler(this.chartRenderer.redrawChart.bind(this.chartRenderer), this.scrollState);
  }
}

// 创建并初始化应用
const app = new GanttApp();
app.init();
